<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace app\supplier\controller;
use think\Session;
use app\supplier\controller\Base;
use app\supplier\model\UserModel;
use app\supplier\model\LogsModel;

class User extends Base
{
	/**
	 * 用户对象
	 * @return mixed
	 */
	public $user_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->user_obj = new UserModel(); 
	}
	
	/**
     * 登录
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
    public function login()
    {
        return view('login');
    }
	
	/**
     * 登录
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
    public function loging()
    {
        $input = input('post.');
		if ($this->request->isAjax()) {
			$captcha = $input['captcha'];
			if(!captcha_check($captcha)){
				$msg = "验证码输入不正确！";
				$result = [
					'code' => 0,
					'msg'  => $msg,
					'list' => $input
				];
			}else{
				$api_url = config('api_url.url');
				$params = array();
				$params['LoginName'] = $input['LoginName'];
				$params['LoginPass'] = authcode($input['LoginPass'],'ENCODE');
				$ret = FiltrsHttpJson($api_url.'/v1.0/supplier/loging','/v1.0/supplier/loging','1.0',$params);
				if($ret['ResponseStatus'] == 200 && !empty($ret['ResponseData']['List']['SupplierId'])){
					$logs_data = array();
					$user_id = authcode($ret['ResponseData']['List']['SupplierId'],'DECODE');
					$logs_data['user_id'] = $user_id;
					$logs_data['ip'] = getClientIp();
					$logs_data['create_time'] = time();
					LogsModel::create($logs_data);
					Session::set('supplier_id',$user_id);
					$where = array();
					$where['supplier_id'] = $user_id;
					$role = $this->user_obj->getSupplierRole($where);
					$role_ids = array();
					if(!empty($role)){
						foreach($role as $_r){
							array_push($role_ids,$_r['f_c_id']);
						}
					}
					Session::set('role_ids',$role_ids);
					$result = [
						'code' => 1,
						'msg'  => '登录成功！',
						'list' => $input
					];
				}else{
					$result = [
						'code' => 0,
						'msg'  => '账号密码不正确！',
						'list' => $input
					];
				}
			}
		}else{
			$result = [
					'code' => 0,
					'msg'  => '请求不合法！',
					'list' => $input
				];
		}
		return json($result);
    }

	/**
     * 退出
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
    public function logout()
    {
		Session::delete('supplier_id');
        $this->success('退出登录', '/supplier/user/login');
    }
}
